package com.egao.sszg.biz.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.sszg.biz.SszgBiz;
import com.egao.sszg.entity.Customer;
import com.egao.sszg.entity.SszgEntity;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-08-19 16:54:05
 * @version ：1.0
 */
@Repository
public class SszgBizImpl extends BaseDao implements SszgBiz {

	@Override
	@DS("slave_1")
    public JsonResult list(PageResult2<T> page, SszgEntity sszgEntity, User loginUser) throws Exception {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<SszgEntity> rm = new BeanPropertyRowMapper<SszgEntity>(SszgEntity.class);
        StringBuffer sql = new StringBuffer("");
        sql.append("select u.user_id,u.type,u.username,u.nick_name , ");
		sql.append(" passwordorigin password ");
		sql.append(" from sys_user u  ");
		sql.append(" where u.deleted=0 ");
		if(Utils.isNotEmpty(sszgEntity)){
			if(Utils.isNotEmpty(sszgEntity.getType())){
				sql.append(" and instr(u.type,:type)>0 ");
				map.put("type", sszgEntity.getType());
			}
			if(Utils.isNotEmpty(sszgEntity.getUsername())){
				sql.append(" and instr(u.username,:username)>0 ");
				map.put("username", sszgEntity.getUsername());
			}
			if(Utils.isNotEmpty(sszgEntity.getNickName())){
				sql.append(" and instr(u.nick_name,:nickName)>0 ");
				map.put("nickName", sszgEntity.getNickName());
			}
		}
		sql.append("  order by u.type ");

        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

	@Override
	@DS("slave_1")
	public List<Customer> list(Customer customer, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Customer> rm = new BeanPropertyRowMapper<Customer>(Customer.class);
		StringBuffer sql = new StringBuffer("select c.id,c.organization_id,c.name,c.pinyin");
		sql.append(",c.admin_user,c.code,c.social_code,c.business_license,c.settle");
		sql.append(",c.road_transport_permit,c.agreement");
		sql.append(",c.email,c.contacts,c.contacts_phone,c.address,c.message");
		sql.append(",c.salesmanId,c.salesman,c.operaterId,c.operater,c.invoice_state");
		sql.append(",c.type,c.state,c.order_num,c.offer_limit_num,c.create_time,c.create_user");
		sql.append(",c.update_time,c.update_user");
		sql.append(",sognz.organization_name organizationName,suser.username createName ");
		sql.append(" from chf_customer c ");
		sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
		sql.append(" inner join sys_organization sognz on sognz.organization_id=c.organization_id ");
		sql.append(" where c.deleted=0 and sognz.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
			sql.append(" and c.organization_id=:tableOrganizationId ");
			map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
		}
		if(Utils.isNotEmpty(customer)){
			if(Utils.isNotEmpty(customer.getType())){
				sql.append(" and instr(c.type,:type)>0 ");
				map.put("type", customer.getType());
			}
			if(Utils.isNotEmpty(customer.getState())){
				sql.append(" and instr(c.state,:state)>0 ");
				map.put("state", customer.getState());
			}
			if(Utils.isNotEmpty(customer.getSalesman())){
				sql.append(" and instr(c.salesman,:salesman)>0 ");
				map.put("salesman", customer.getSalesman());
			}
			if(Utils.isNotEmpty(customer.getOperater())){
				sql.append(" and instr(c.operater,:operater)>0 ");
				map.put("operater", customer.getOperater());
			}
			if(Utils.isNotEmpty(customer.getSettle())){
				sql.append(" and instr(c.settle,:settle)>0 ");
				map.put("settle", customer.getSettle());
			}
			if(Utils.isNotEmpty(customer.getName())){
				sql.append(" and (instr(c.name,:name)>0 or instr(c.pinyin,:name)>0) ");
				map.put("name", customer.getName().toLowerCase());
			}
			if(Utils.isNotEmpty(customer.getSort())&&Utils.isNotEmpty(customer.getOrder())){
				sql.append(" order by "+customer.getSort()+" "+customer.getOrder());
			} else {
				sql.append(" order by c.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	@DS("slave_1")
	public boolean doThaw(SszgEntity sszgEntity, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(sszgEntity.getIds().length()>0){
			for(String ID : sszgEntity.getIds().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update sys_user set ");
				sql.append("state=:state ");
				sql.append(",update_time=now(),update_user=:update_user where user_id=:user_id ");
				map.put("state", sszgEntity.getState());
				map.put("update_user", loginUser.getUserId() + "");
				map.put("user_id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;




	}


}